﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hi_Baidu.ProcessModel;

namespace Hi_Baidu.Timer
{
    /// <summary>
    ///     提供了对于心跳包的超时计算以及相关操作
    /// </summary>
    class HeartBreathTimeOutEvent : TimeOutEvent
    {
        private String username;
        /// <summary>
        ///     提供了对于心跳包的超时计算以及相关操作
        /// </summary>
        /// <param name="Key">唯一标示</param>
        /// <param name="Interval">触发间隔</param>
        public HeartBreathTimeOutEvent(String username, String Key, int Interval)
            : base(Key, Interval, -1)
        {
            this.username = username;
            this.OnTick += new EventHandler<EventArgs>(HeartBreathTimeOutEvent_OnTick);
        }

        void HeartBreathTimeOutEvent_OnTick(object sender, EventArgs e)
        {
           //尝试发送心跳包
            UserProcessModel.SendHeartBreath(Encoding.Default.GetBytes(this.username));
            //创建反馈心跳超时器
            UserHeartBreathTimeOutEvent userHeartBreathTimeOutEvent = new UserHeartBreathTimeOutEvent("Reply_UserHeartBreath", 5000, 1);
            //打入超时器
            Hi_Baidu.Globle.timeoutTimerManager.InstallTimer(userHeartBreathTimeOutEvent);
        }
    }
}
